//
//  ZDSearchBar.m
//  0921仿网易新闻框架01
//
//  Created by Apple on 15/9/23.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "ZDSearchBar.h"
#import "UIImage+Extension.h"

#define padding 10

#define searchImage [UIImage imageNamed:@"searchbar_textfield_search_icon"]

#define ZDPlaceholderW [self.placeholder boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.width


@implementation ZDSearchBar

+ (instancetype)searchBar
{
    return [[self alloc] init];
}

//重写构造方法，添加子控件，并设置相关属性
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //设置leftView
        UIImageView *searchView = [[UIImageView alloc] initWithImage:searchImage];
        
        self.leftView = searchView;
        
        self.leftViewMode = UITextFieldViewModeAlways;
        
        //设置textField的边框
        self.background = isiOS7 ? [UIImage resizeImageWithImageName:@"searchbar_textfield_background_os7"] :[UIImage resizeImageWithImageName:@"searchbar_textfield_background"];
        
        //设置placeholder
        self.placeholder = @"选份礼物给最爱的人";     //默认字体是18
        
        //修改字体为15
        self.font = [UIFont systemFontOfSize:15];
        
        //设置textField的清除按钮模式
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        //设置键盘
        self.keyboardType = UIKeyboardTypeNamePhonePad;
        
        //设置键盘returnKey
        self.returnKeyType = UIReturnKeySearch;
        
        self.enablesReturnKeyAutomatically = YES;
        
        //设置textField的bounds  ———— 设置成navigationItem的titleView,只需要设置宽高即可
        self.bounds = CGRectMake(0, 0, 320, 30);    //30是textField的默认高度
    }
    
    return self;
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
//    self.bounds = CGRectMake(0, 0, 300, 30);
//    
////    self.leftView.frame = CGRectMake(padding, (self.frame.size.height - self.leftView.frame.size.height)*0.5, self.leftView.frame.size.width, self.leftView.frame.size.height);
////    
////    ZDLog(@"self.leftView.frame = %@", NSStringFromCGRect(self.leftView.frame));
//}


//重新布局leftView的frame  1
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
    CGFloat imageW = searchImage.size.width;
    CGFloat imageH = searchImage.size.height;
    
    if (self.isEditing)
    {
        return CGRectMake(padding, (bounds.size.height - imageH)*0.5, imageW, imageH);
    }
    
    CGFloat leftViewX = (bounds.size.width - imageW - ZDPlaceholderW - padding * 0.5) * 0.5;
    return CGRectMake(leftViewX, (bounds.size.height - imageH)*0.5, imageW, imageH);
}

//重新布局编辑框的frame   3
- (CGRect)editingRectForBounds:(CGRect)bounds
{
    CGFloat editX = CGRectGetMaxX(self.leftView.frame) + padding * 0.5;
    CGFloat editY = 0;
    CGFloat editW = bounds.size.width - editX;
    CGFloat editH = bounds.size.height;
    
    return CGRectMake(editX, editY, editW, editH);
}

//重新布局placeholder的frame  2
- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
//    ZDLog(@"bounds = %@", NSStringFromCGRect(bounds));
//    
//    ZDLog(@"self.leftView.frame = %@", NSStringFromCGRect(self.leftView.frame));
    
    CGFloat placeholderX = CGRectGetMaxX(self.leftView.frame) + padding * 0.5;
    CGFloat placeholderY = 0;
    CGFloat placeholderW = ZDPlaceholderW;    //传入上面设置的字体大小，计算宽度
    
    CGFloat placeholderH = self.frame.size.height;
    
//    ZDLog(@"placeholderRect = %@", NSStringFromCGRect(CGRectMake(placeholderX, placeholderY, placeholderW, placeholderH)));
    
    return CGRectMake(placeholderX, placeholderY, placeholderW, placeholderH);
}


@end
